package com.newer.weatherforecast.dao;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import com.newer.weatherforecast.entity.CityInfo;
import com.newer.weatherforecast.ui.R;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;

public class CityDAO extends BaseDAO{
	private final int BUFFER_SIZE = 400000;
	public static final String DB_NAME = "mycity.db";
	public static final String PACKAGE_NAME = "com.newer.weatherforecast.ui";
	public static final String DB_PATH = "/data"+ Environment.getDataDirectory().getAbsolutePath() + "/"+ PACKAGE_NAME;
	private SQLiteDatabase database;
	private Context context;
	private static final String CITY_NAME = "cityname";
	private static final String PY_CITY_NAME = "pycityname";
	private static final String PY_SHORT = "pyshort";
	private static final String ADD_CITY = "city";
	public static final String CITYINFO_NAME = "city_name";
	public static final String CITYINFO_CURRENT_CITY = "current_city";
	public static final String CITYINFO_LAST_UPDATE_TIME = "last_update_time";
	public static final String CITYINFO_LAST_TEMPRETURE = "last_tempreture";
	public static final String CITYINFO_LAST_WEATHER = "last_weather";
	public static final String CITYINFO_LAST_ICON = "last_icon";
	public CityDAO(Context context) {
		super(context);
		this.context = context;
		this.openDatabase();
	}
	
	public CityInfo[] QueryMyCity(){
		CityInfo[] cityInfos = null;
		Cursor cur_city = sqlDB.query(CITY, null, null, null, null, null, null);
		int count = cur_city.getCount();
		if(count==0||!cur_city.moveToFirst()){
			cur_city.close();
			return null;
		}
		cityInfos = new CityInfo[count];
		for (int i = 0; i < cityInfos.length; i++) {
			cityInfos[i] = new CityInfo();
			cityInfos[i].setCity_name(cur_city.getString(1));
			if(cur_city.getString(2).equals("1")){
				cityInfos[i].setCurrent_city(true);
			}else {
				cityInfos[i].setCurrent_city(false);
			}
			cityInfos[i].setLast_update_time(cur_city.getString(3));
			cityInfos[i].setLast_tempreture(cur_city.getString(4));
			cityInfos[i].setLast_weather(cur_city.getString(5));
			cityInfos[i].setIcon(cur_city.getInt(6));
			cur_city.moveToNext();
		}
		cur_city.close();
		return cityInfos;
	}
	
	public long clearCurrentCity(){
		ContentValues values = new ContentValues();
		values.put(CITYINFO_CURRENT_CITY,0);
		return sqlDB.update(CITY, values, CITYINFO_CURRENT_CITY+"=1", null);
	}

	public long updateCurrentCity(int id){
		ContentValues values = new ContentValues();
		values.put(CITYINFO_CURRENT_CITY,1);
		return sqlDB.update(CITY, values, "_id="+(id+1), null);
	}
	
	public long delCityById(int id){
		return sqlDB.delete(CITY, "_id="+(id+1), null);
	}
	
	public long insertCity(CityInfo cityInfo){
		ContentValues values = new ContentValues();
		values.put(CITYINFO_NAME, cityInfo.getCity_name());
		if (cityInfo.isCurrent_city()) {
			values.put(CITYINFO_CURRENT_CITY,1);
		}else {
			values.put(CITYINFO_CURRENT_CITY,0);
		}
		values.put(CITYINFO_LAST_UPDATE_TIME,cityInfo.getLast_update_time());
		values.put(CITYINFO_LAST_TEMPRETURE,cityInfo.getLast_tempreture());
		values.put(CITYINFO_LAST_WEATHER, cityInfo.getLast_weather());
		values.put(CITYINFO_LAST_ICON, cityInfo.getIcon());
		return sqlDB.insert(CITY, null, values);
	}
	
	public long updateCityWeather(CityInfo cityInfo,int id){
		ContentValues values = new ContentValues();
		values.put(CITYINFO_LAST_UPDATE_TIME,cityInfo.getLast_update_time());
		values.put(CITYINFO_LAST_TEMPRETURE,cityInfo.getLast_tempreture());
		values.put(CITYINFO_LAST_WEATHER, cityInfo.getLast_weather());
		values.put(CITYINFO_LAST_ICON, cityInfo.getIcon());
		return sqlDB.update(CITY, values, "_id="+(id+1), null);
	}
	
//	public List<String> getSelectCity(String key){   
//    	database = SQLiteDatabase.openDatabase(DB_PATH+"/"+DB_NAME,null,SQLiteDatabase.OPEN_READONLY);
//    	List<String> citys = selectCity(key);
//    	database.close();
//    	return citys;
//	}
	public List<String> getSelectCity(String key){   
	    	List<String> cityList = new ArrayList<String>();   
	    	Cursor cur_querycity = null;  
	    	if(key!=null&&!key.equals("")){
			String [] columns = {CITY_NAME};
			String where =  CITY_NAME+ " like ? or "+PY_CITY_NAME+" like ? or "+PY_SHORT+" like ? ";
			String [] selectArgs = {key+"%" , key+"%" , key+"%"};    
			cur_querycity = database.query(ADD_CITY, columns, where , selectArgs, null, null, null);   
			if(!cur_querycity.moveToFirst()||cur_querycity==null){
				cur_querycity.close();
				return null;
			}
			cur_querycity.moveToFirst();
			 while(cur_querycity.moveToNext()){   
				 String name =  cur_querycity.getString(0);   
				 cityList.add(name);
			 }
			 cur_querycity.close();
		     return cityList;
		}
		return null;
	}
	public CityInfo getCurrentCity(){
		Cursor cursor = sqlDB.query(CITY, null, "current_city = 1", null, null, null, null);
		int count = cursor.getCount();
		if(count==0||!cursor.moveToFirst()){
			cursor.close();
			return null;
		}
		CityInfo cityInfo = new CityInfo();
		cityInfo.setCity_name(cursor.getString(1));
		if(cursor.getString(2).equals("1")){
			cityInfo.setCurrent_city(true);
		}else{
			cityInfo.setCurrent_city(false);
		}
		cityInfo.setLast_update_time(cursor.getString(3));
		cityInfo.setLast_tempreture(cursor.getString(4));
		cityInfo.setLast_weather(cursor.getString(5));
		cityInfo.setIcon(cursor.getInt(6));
		cursor.close();
		return cityInfo;
	}
	public String getCurrentCityName(){
		Cursor cursor = sqlDB.query(CITY, new String[]{"city_name"}, "current_city = 1", null, null, null, null);
		int count = cursor.getCount();
		if(count==0||!cursor.moveToFirst()){
			cursor.close();
			return null;
		}
		String cityName = cursor.getString(0);
		cursor.close();
		String pyCityName = getPyCityName(cityName);
		return pyCityName;
	}
	
	public String getZhCityName(String key){
		Cursor cursor = database.query(ADD_CITY, new String[]{CITY_NAME}, PY_CITY_NAME+"=?", new String[]{key}, null, null, null);
		int count = cursor.getCount();
		if(count==0||!cursor.moveToFirst()){
			cursor.close();
			return null;
		}
		String zhCityName = cursor.getString(0);
		cursor.close();
		return zhCityName;
	}
	
//	public String getZhCityName(String key){
//		database = SQLiteDatabase.openDatabase(DB_PATH+"/"+DB_NAME,null,SQLiteDatabase.OPEN_READONLY);
//    	String zhCityName = zhCityName(key);
//    	database.close();
//    	return zhCityName;
//	}
	
	public String getPyCityName(String key){
		Cursor cursor = database.query(ADD_CITY, new String[]{PY_CITY_NAME}, CITY_NAME+"=?", new String[]{key}, null, null, null);
		int count = cursor.getCount();
		if(count==0||!cursor.moveToFirst()){
			cursor.close();
			return null;
		}
		String pyCityName = cursor.getString(0);
		cursor.close();
		return pyCityName;
	}
	
//	public String getPyCityName(String key){
//		database = SQLiteDatabase.openDatabase(DB_PATH+"/"+DB_NAME,null,SQLiteDatabase.OPEN_READONLY);
//    	String pyCityName = pyCityName(key);
//    	database.close();
//    	return pyCityName;
//	}
	
	public void openDatabase(){
		this.database = this.openDatabase(DB_PATH+"/"+DB_NAME);
	}
	private SQLiteDatabase openDatabase(String dbfile){
		try {
			 if (!(new File(dbfile).exists())) {
				 InputStream is = this.context.getResources().openRawResource(
						 R.raw.mycity);
				 FileOutputStream fos = new FileOutputStream(dbfile);
				 byte[] buffer = new byte[BUFFER_SIZE];
				 int count = 0; 
				 while ((count = is.read(buffer)) > 0) { 
					 fos.write(buffer, 0, count); 
				 } 
				 fos.close();
				 is.close();
			 }
			 return SQLiteDatabase.openDatabase(dbfile, null, SQLiteDatabase.OPEN_READONLY); 
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	public void closeDatabase(){
		if(database!=null){
			this.database.close();
		}
	}
}
